﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using WMPRemote;

namespace WmpRemoteServer
{
    public partial class Form1 : Form
    {
        private WindowsMediaPlayer wmp;
        private bool mute = false;
        public Form1()
        {
            InitializeComponent();
            RemotedWindowsMediaPlayer player = new RemotedWindowsMediaPlayer();
            player.CreateControl();
            wmp = player.GetOcx() as WindowsMediaPlayer;
            updateData();
        }

        private void updateData()
        {
            IWMPMedia tmp = wmp.currentMedia;
            string currentMediaInfo = "";
            for (int i = 0; i < tmp.attributeCount; i++)
            {
                string t = tmp.getAttributeName(i);
                currentMediaInfo += "\n" + t + ": " + tmp.getItemInfo(t);
            }
            //TestLabel.Text = s;
            richTextBox1.Text = currentMediaInfo;
            //wmp.currentPlaylist;
            //wmp.mediaCollection;
            //wmp.playlistCollection;
            string info = "CurrentPossition: " + wmp.controls.currentPosition;
            info += "\nCurrentPossitionString: " + wmp.controls.currentPositionString;
            info += "\nPlayer status: " + wmp.status;//Pause,Stop,Playing etc...
            info += "\nVolume: " + wmp.settings.volume;
            info += "\nMute: " + wmp.settings.mute;
            info += "\nPlayStat: " + wmp.playState;

            //IWMPPlaylistArray pList = wmp.playlistCollection.getAll();


            string playListInfo = "";
            //for (int i = 0; i < pList.count; i++)
            //{
                IWMPPlaylist list = wmp.currentPlaylist;
                playListInfo += "\nListC: " + list.count;
                for (int k = 0; k < list.count; k++)
                {
                    try
                    {
                        IWMPMedia m = list.get_Item(k);
                        comboBox1.Items.Add(m);
                        playListInfo += "\n" + m.name;
                    }
                    catch (Exception)
                    {
                    }
                }
            //}
            comboBox1.DisplayMember = "name";
            comboBox1.SelectedValueChanged +=new EventHandler(comboBox1_SelectedValueChanged);
            //for (int i = 0; i < collection.count; i++)
            //{
            //    //string t = pList.get_attributeName(i);
            //    //playListInfo += "\n" + t + ": " + pList.getItemInfo(t);
            //    playListInfo += "\n" + collection.Item(i).name;
            //    if(collection.Item(i).name.Equals("All Music")){
            //        pList = collection.Item(i);
            //        for(int k = 0; k < pList.count; k++){
            //            String p = pList.get_attributeName(i);
            //            //playListInfo += "\n" + p + ": " + pList.getItemInfo(p);
            //            IWMPMedia m = pList.get_Item(i);
            //            playListInfo += "\n" + m.name;
            //        }
            //    }

            //}
            richTextBox2.Text = playListInfo;
            TestLabel.Text = info;
            wmp.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(_player_PlayStateChange);
            wmp.MediaChange+= new _WMPOCXEvents_MediaChangeEventHandler(wmp_MediaChange); 
        }

        public void wmp_MediaChange(object Item)
        {
            IWMPMedia m = Item as IWMPMedia;
            TestLabel.Text = m.name;
        }
        public void _player_PlayStateChange(int NewState)
        {
            
            switch (NewState)
            {
                case 0:    // Undefined
                    currentStateLabel.Text = "Undefined";
                    break;

                case 1:    // Stopped
                    currentStateLabel.Text = "Stopped";
                    break;

                case 2:    // Paused
                    currentStateLabel.Text = "Paused";
                    break;

                case 3:    // Playing
                    currentStateLabel.Text = "Playing";
                    break;

                case 4:    // ScanForward
                    currentStateLabel.Text = "ScanForward";
                    break;

                case 5:    // ScanReverse
                    currentStateLabel.Text = "ScanReverse";
                    break;

                case 6:    // Buffering
                    currentStateLabel.Text = "Buffering";
                    break;

                case 7:    // Waiting
                    currentStateLabel.Text = "Waiting";
                    break;

                case 8:    // MediaEnded
                    currentStateLabel.Text = "MediaEnded";
                    break;

                case 9:    // Transitioning
                    currentStateLabel.Text = "Transitioning";
                    break;

                case 10:   // Ready
                    currentStateLabel.Text = "Ready";
                    break;

                case 11:   // Reconnecting
                    currentStateLabel.Text = "Reconnecting";
                    break;

                case 12:   // Last
                    currentStateLabel.Text = "Last";
                    break;

                default:
                    currentStateLabel.Text = "Unknown State";
                    break;
            }
        }
        public void test()
        {
        }

        public void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            IWMPMedia item = (IWMPMedia)comboBox1.SelectedItem;
            wmp.controls.playItem(item);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            updateData();
            //IWMPMedia m = wmp.newMedia("@D:\\Music\\Van Canto\\Van Canto - A Storm To Come\\09 - Battery.mp3");
            //wmp.controls.playItem(m);
        }

        private void Play_Click(object sender, EventArgs e)
        {
            wmp.controls.play();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            wmp.controls.stop();
        }

        private void Pause_Click(object sender, EventArgs e)
        {
            wmp.controls.pause();
        }

        private void Next_Click(object sender, EventArgs e)
        {
            wmp.controls.next();
        }

        private void FastForward_Click(object sender, EventArgs e)
        {
            wmp.controls.fastForward();
        }

        private void Previous_Click(object sender, EventArgs e)
        {
            wmp.controls.previous();
        }

        private void FastReverse_Click(object sender, EventArgs e)
        {
            wmp.controls.fastReverse();
        }

        private void MuteButton_Click(object sender, EventArgs e)
        {
            mute = !mute;
            wmp.settings.mute = mute;
        }

        private void VolumeUp_Click(object sender, EventArgs e)
        {
            int volume = wmp.settings.volume;
            wmp.settings.volume = volume + 10;
        }

        private void VolumeDown_Click(object sender, EventArgs e)
        {
            int volume = wmp.settings.volume;
            wmp.settings.volume = volume - 10;
        }
    }
}
